//
//  BIDWithHttpRequestViewController.m
//  zhangqitong
//
//  Created by mal on 15/11/30.
//  Copyright © 2015年 mal. All rights reserved.
//

#import "BIDWithHttpRequestViewController.h"
#import "BIDLoginViewController.h"

@interface BIDWithHttpRequestViewController ()

@end

@implementation BIDWithHttpRequestViewController
@synthesize completionHandler;
@synthesize bManualHandleHTTPFailed;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**
 以post方式从后台请求数据

 @param requestURL 请求地址
 @param postData post参数
 @param completion 请求完数据的回调，errMsg为nil时，说明请求成功，否则errMsg为错误信息
 */
- (void)sendHttpRequestWithURL:(NSString *)requestURL postData:(NSString *)postData withCompletion:(void (^)(NSDictionary *, NSString *))completion
{
    BIDDataCommunication *dc = [[BIDDataCommunication alloc] init];
    [self showSpinnerView];
    NSURLConnection *connection = [dc getDataFromNet:requestURL postValue:postData completionHandler:^(id obj, NSError *err){
        [self hideSpinnerView];
        if([self respondsToSelector:@selector(externalTask)])
        {
            //主要是baseListView用来执行endRefresh方法
            [self externalTask];
        }
        if(obj && !err)
        {
            if([obj isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *dic = (NSDictionary*)obj;
                NSDictionary *headDic = dic[@"head"];
                NSNumber *value = headDic[@"success"];
                //有些接口没有返回head字段
                if(ISNULL(headDic))
                {
                    headDic = dic;
                    value = headDic[@"success"];
                }
                if([value intValue] == 0)
                {
                    //只取错误提示信息
                    NSString *strMsg = headDic[@"msg"];
                    if(!ISNULL(strMsg))
                    {
                        completion(nil, strMsg);
                    }
                    else
                    {
                        if(bManualHandleHTTPFailed)
                        {
                            //手动处理错误信息的例外
                            completion(headDic, nil);
                        }
                        else
                        {
                            completion(nil, @"请求失败,请稍后重试");
                        }
                    }
                }
                else
                {
                    //http请求执行成功
                    completion(dic[@"body"], nil);
                }
            }
            else if([obj isKindOfClass:[NSArray class]])
            {
                completion(obj, nil);
            }
            else
            {
                completion(nil, @"数据不是有效的格式");
            }
        }
        else
        {
            completion(nil, [err localizedDescription]);
        }
    }];
    [self.networkConnectionArr addObject:connection];
}

@end
